package com.winning.pmph.ctrl;

import com.winning.pmph.utils.ApplicationContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.awt.image.ToolkitImage;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
@Api(value = "获取图片")
@Controller
@RequestMapping("/ymer/action/file")
public class YmerFileCtrl {

    public static BufferedImage getResizedWaterMark(BufferedImage distImg) {
        BufferedImage waterMark = ApplicationContextUtil.getBean(BufferedImage.class);
        int waterMarkWidth = (int) Math.round(distImg.getWidth() * 0.8);
        ToolkitImage tmp = (ToolkitImage) waterMark.getScaledInstance(waterMarkWidth, -1, Image.SCALE_SMOOTH);
        BufferedImage resizedWaterMark = new BufferedImage(tmp.getWidth(), tmp.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = resizedWaterMark.createGraphics();
        g2d.drawImage(tmp, 0, 0, null);
        g2d.dispose();
        return resizedWaterMark;
    }

    @ApiOperation(value = "showImageWithoutWaterMark", notes = "无水印图片")
    @GetMapping("/showImageWithoutWaterMark")
    public void showImageWithoutWaterMark(String filename, HttpServletResponse response) throws IOException {
        File file = new File(ApplicationContextUtil.BASE_DOWN_FORDER + filename);
        if (file.exists()) {
            response.setContentType("image/png");
            response.getOutputStream().write(FileUtils.readFileToByteArray(file));
            response.getOutputStream().flush();
        }
    }

    public static BufferedImage addWaterMark(Image srcImg) {
        BufferedImage bufferedImage = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null),
                BufferedImage.TYPE_4BYTE_ABGR);

        // 得到画笔对象
        Graphics2D g = bufferedImage.createGraphics();
        // 设置对线段的锯齿状边缘处理
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0,
                0, null);
        BufferedImage waterMark = getResizedWaterMark(bufferedImage);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.1f));
        g.drawImage(waterMark, bufferedImage.getWidth() / 10, (bufferedImage.getHeight() - waterMark.getHeight()) / 2, null);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        g.dispose();
        return bufferedImage;
    }

    @ApiOperation(value = "showImage", notes = "获取图片接口")
    @GetMapping("/showImage")
    public void showImage(String filename,HttpServletResponse response) throws IOException {
        File file = new File(ApplicationContextUtil.BASE_DOWN_FORDER + filename);
        if (file.exists()) {
            Image srcImg = ImageIO.read(file);
            if (srcImg.getWidth(null) < 200) {
                showImageWithoutWaterMark(filename, response);
                return;
            }
            BufferedImage bufferedImage = addWaterMark(srcImg);
//            ImageIO.write(bufferedImage, "png", response.getOutputStream());
            if (!StringUtils.substringAfterLast(filename, ".").equals("png")) {
                Thumbnails.of(bufferedImage)
                        .scale(1f).outputQuality(0.7f)
                        .outputFormat("jpg").toOutputStream(response.getOutputStream());
            } else {
                ImageIO.write(bufferedImage, "png", response.getOutputStream());
            }
            response.getOutputStream().flush();
        }
    }

    @ApiOperation(value = "showEditor135Image", notes = "获取135图片接口")
    @GetMapping("/showEditor135Image")
    public void showEditor135Image(String filename, String foldercode,HttpServletResponse response) throws IOException {
        if (StringUtils.isNotBlank(foldercode)) {
            filename = foldercode + File.separator + filename;
        }
        String filePath = ApplicationContextUtil.BASE_DOWN_FORDER + filename;
        response.setContentType("image/png");
        response.getOutputStream().write(FileUtils.readFileToByteArray(new File(filePath)));
        response.getOutputStream().flush();
        File file = new File(ApplicationContextUtil.BASE_DOWN_FORDER + filename);
        if (file.exists()) {
            response.setContentType("image/png");
            response.getOutputStream().write(FileUtils.readFileToByteArray(file));
            response.getOutputStream().flush();
        }
    }
}
